﻿
-----------------------------------------------------------------------
-- MatrixChannel.lua
--
-- A Framework for World Of Warcraft addons
-- Copyright (C) 2007  wwkkii
-- 
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
-- 
-- Home Page: http://code.google.com/p/wowmatrix
-----------------------------------------------------------------------

local __version__ = { "MatrixChannel", 0, 1, 1 };
if (MatrixUpdateVersion(__version__)) then return; end

-------------------------------------------------------------------------------
-- local variables
-------------------------------------------------------------------------------

local chat_frame_hooker;
local MATRIX_CHANNEL_NAME = "MATRIXMSG";

-------------------------------------------------------------------------------
-- local callbacks
-------------------------------------------------------------------------------

local function Matrix_OnBinaryMessage()
end

local function Matrix_OnCharFrameEvent(event)
	if ((event == "CHAT_MSG_CHANNEL") and (arg9 == MATRIX_CHANNEL_NAME)) then
		return;
	end
	chat_frame_hooker(event);
end

local function Matrix_OnAddonMessage()
	matrix.Print("Here addon arg1:"..tostring(arg1)..";arg2:"..tostring(arg2)..";arg3:"..tostring(arg3)..";arg4:"..tostring(arg4));
end

local function Matrix_OnChannelMessage()
	if (arg9 ~= MATRIX_CHANNEL_NAME) then
		return;
	end
	matrix.Print(arg1);
end

local function Matrix_OnMessageInitial()
	matrix.Print("Here message initial.");
	matrix.AddEvent("CHAT_MSG_ADDON", Matrix_OnAddonMessage);
	matrix.AddEvent("CHAT_MSG_CHANNEL", Matrix_OnChannelMessage);
	JoinChannelByName(MATRIX_CHANNEL_NAME);
	chat_frame_hooker = MatrixHooker:New("ChatFrame_OnEvent", Matrix_OnChatFrameEvent);
end

local function Matrix_OnSendCommand(info)
	matrix.SendMessage("std", "MATRIX", info);
end

-------------------------------------------------------------------------------
-- MatrixChannel Interface
-------------------------------------------------------------------------------

MatrixInterface("MatrixChannel");

function MatrixChannel:Listen(mode, name)
	if (not name) then
		name = self:Name();
	end
end

function MatrixChannel:SendPacket(mesg, target)
	if (target == nil) then
	elseif (target == "GUILD") then
		SendAddonMessage("MAT_"..self:Name(), msg, target, nil);
	elseif ((target == "RAID") or (target == "PARTY") or (target == "BATTLEGROUND")) then
		SendAddonMessage("MAT_"..self:Name(), msg, target, nil);
	elseif (target == "MATRIX") then
		SendChatMessage(self:Name().." "..tostring(msg), "CHANNEL", nil, GetChannelName(MATRIX_CHANNEL_NAME));
	else
		SendAddonMessage("MAT_"..self:Name(), msg, "WHISPER", target);
	end
end

function MatrixChannel:OnReceive(mesg, sender, mode)
end
